package com.zyd.blog.homeart.util;

import com.zyd.blog.homeart.service.ICommentReplyService;
import com.zyd.blog.homeart.service.ICommentService;
import com.zyd.blog.homeart.service.IDesignArticleService;
import com.zyd.blog.homeart.service.IRotationChartService;
import com.zyd.blog.homeart.service.es.esDesignArticleService;
import com.zyd.blog.persistence.beans.es.EsDesignArticle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

@Component
public class UuidUtil {
    private final Logger logger = LoggerFactory.getLogger(UuidUtil.class);
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private IDesignArticleService iDesignArticleService;
    @Autowired
    private esDesignArticleService esDesignArticleService;

    /**
     * 导入所有姓氏到ES
     */
    public ResponseObject importSongData() {
        logger.info("/commentReply/importSurnameOrigin 入所有姓氏到ES");
        int pageIndex = 1;
        int pageSize = 2000;
        while (true) {
            List<EsDesignArticle> list = iDesignArticleService.getEsDesignArticle(pageIndex, pageSize);
            if (list == null || list.size() == 0) {
                break;
            }
            try {
                int count = 0;
                if (elasticsearchTemplate.indexExists(EsDesignArticle.class)) {
                    elasticsearchTemplate.createIndex(EsDesignArticle.class);
                    elasticsearchTemplate.putMapping(EsDesignArticle.class);
                }
                ArrayList<EsDesignArticle> esDesignArticles = new ArrayList<>();
                for (EsDesignArticle esDesignArticle : list) {
                    esDesignArticles.add(esDesignArticle);
                    if (count % 500 == 0) {
                        esDesignArticleService.saveAll(esDesignArticles);
                        esDesignArticles.clear();
                    }
                    count++;
                }
                //记得提交整除余数不为0时的那部分数据
                if (esDesignArticles.size() > 0) {
                    esDesignArticleService.saveAll(esDesignArticles);
                }
            } catch (Exception e) {
                e.printStackTrace();
                ResponseObject reObj = new ResponseObject();
                reObj.setCode(2);
                reObj.setMsg("导入失败");
                return reObj;
            }
            pageIndex++;
        }
        ResponseObject reObj = new ResponseObject();
        reObj.setCode(0);
        reObj.setMsg("导入成功");
        return reObj;
    }

}

